/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: yang
 * Date: 2022-07-05
 * Time: 23:34
 */
// 2、无头双向链表实现
public class MyLinkedList {
    static class ListNode{
        public int val;
        public ListNode prev;
        public ListNode next;
        public ListNode(int val){
            this.val = val;
            prev = null;
            next = null;
        }

    }

    int size;
    ListNode head;
    ListNode last;

    public MyLinkedList(){
        size = 0;
        head = null;
        last = null;
    }

    //头插法
    public void addFirst(int data){
        if(head == null){
            ListNode node = new ListNode(data);
            head = node;
            last = node;
        }
        else{
            ListNode node = new ListNode(data);
            head.prev = node;
            node.next = head;
            head = node;
        }
        size++;
    }
    //尾插法
    public void addLast(int data){
        if(head == null){
            ListNode node = new ListNode(data);
            head = node;
            last = node;
        }
        else{
            ListNode node = new ListNode(data);
            last.next = node;
            node.prev = last;
            last = node;
        }
        size++;
    }
    //任意位置插入,第一个数据节点为0号下标
    public void addIndex(int index,int data){
        if(index < 0 || index > size)throw new IndexNotLegalException("下标位置不合法！");
        if(index == 0){
            addFirst(data);
            return;
        }
        if(index == size){
            addLast(data);
            return;
        }
        ListNode tmp = head;
        while(index-- != 0){
            tmp = tmp.next;
        }
        ListNode node = new ListNode(data);
        node.next = tmp.next;
        node.prev = tmp;
        tmp.next = node;
        size++;
    }
    //查找是否包含关键字key是否在单链表当中
    public boolean contains(int key){
        ListNode tmp = head;
        while(tmp != null){
            if(key == tmp.val)return true;
            tmp = tmp.next;
        }
        return false;
    }
    //删除第一次出现关键字为key的节点
    public void remove(int key){
        ListNode tmp = head;
        while(tmp != null){
            if(key == tmp.val){
                tmp.prev.next = tmp.next;
                tmp.next.prev = tmp.prev;
                size--;
                return;
            }
            tmp = tmp.next;
        }
    }

    //删除所有值为key的节点
    public void removeAllKey(int key){
        ListNode tmp = head;
        while(tmp != null){
            if(key == tmp.val){
                tmp.prev.next = tmp.next;
                tmp.next.prev = tmp.prev;
                size--;
                tmp = tmp.prev;
            }
            tmp = tmp.next;
        }
    }
    //得到单链表的长度
    public int size(){
        return size;
    }
    public void display(){
        ListNode tmp = head;
        while(tmp != null){
            System.out.print(tmp.val + " ");
            tmp = tmp.next;
        }
        System.out.println();
    }
    public void clear(){
        head = null;
        last = null;
        size = 0;
    }

    public static void main(String[] args) {
        MyLinkedList list = new MyLinkedList();
        list.addLast(10);
        list.addFirst(20);
        list.display();
        list.addIndex(0,30);
        list.display();
        list.remove(30);
        list.display();
    }
}
